package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeSectionParticipantService;
import com.bidevalution.dao.entity.BeSectionDecrypt;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.BeSectionParticipant;
import com.qtp.common.core.ContextHolderUtils;
import com.qtp.common.core.TSUser;
import com.qtp.core.model.Paging;
import com.qtp.core.model.Response;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 投标参与控制层
 * @author shenh@glodon.com
 * @date 2019-04-29 10:39
 */
@Log4j2
@RestController
@RequestMapping("/beSectionParticipant")
public class BeSectionParticipantController extends BaseController {
    @Autowired
    private BeSectionParticipantService beSectionParticipantService;
    /**
     * 获取总的投标单位参与家数
     * @param ptSectionId 平台标段Id
     * @return
     */
    @RequestMapping(value = "/queryParticipantCount", method = RequestMethod.GET)
    @ResponseBody
    public Response<Long> queryParticipantCount(@RequestParam("ptSectionId")String ptSectionId) {
        try {
        	return Response.ok(beSectionParticipantService.queryParticipantCount(ptSectionId));
        } catch (Exception e){
            log.error(e.getMessage());
            return  Response.fail("获取投标单位总数失败");
        }
    }

    /**
     * 获取已报名的投标单位信息
     * @param sectionId 平台标段Id
     * @return
     */
    @RequestMapping(value = "/queryParticipantList", method = RequestMethod.GET)
    @ResponseBody
    public Response<List<Map<String, String>>> queryParticipantList(@RequestParam("sectionId")String sectionId) {
        try {
            return Response.ok(beSectionParticipantService.queryParticipantList(sectionId));
        } catch (Exception e){
            log.error(e.getMessage());
            return  Response.fail("获取投标单位列表信息失败");
        }
    }


    /**
     * 获取单个投标单位信息
     * @param sectionId 标段Id
     * @param orgCode 企业组织机构代码
     * @return
     */
    @RequestMapping(value = "/getParticipantInfo", method = RequestMethod.POST)
    @ResponseBody
    public void getParticipantInfo(HttpServletResponse response, @RequestParam("sectionId")String sectionId, @RequestParam("orgCode")String orgCode) {
        try {
            setHttpServletResponse(response);
            BeSectionParticipant beSectionParticipant = beSectionParticipantService.getParticipantInfo(sectionId,orgCode);
            List<BeSectionParticipant> beSectionParticipants = new ArrayList<>();
            beSectionParticipants.add(beSectionParticipant);
            Paging<List<BeSectionInfo>> btnResponse = new Paging(Long.valueOf(beSectionParticipants.size()), beSectionParticipants);
            long total = btnResponse.getTotal();
            ContextHolderUtils.outGridJson(response, btnResponse.getData(), total,"yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            log.error(e.getMessage());
       }
    }

    /**
     * 获取投标单位信息列表
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/getParticipantList")
    @ResponseBody
    public void getParticipantList(HttpServletResponse response, @RequestParam("sectionId")String sectionId,
                                                                           @RequestParam(value="participantName",required=false)String participantName,
                                                                           @RequestParam("page")int page, @RequestParam("rp")int rp) {
        try {
            setHttpServletResponse(response);
            Paging<List<BeSectionParticipant>> btnResponse = beSectionParticipantService.getParticipantList(sectionId,participantName,page,rp);
            long total = btnResponse.getTotal();
            ContextHolderUtils.outGridJson(response, btnResponse.getData(), total,"yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
           log.error(e.getMessage());
        }
    }

    /**
     * 展示报价
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/showPrice", method = RequestMethod.POST)
    @ResponseBody
    public Response<Boolean> showPrice(@RequestParam("sectionId")String sectionId) {
        try {
            return Response.ok(beSectionParticipantService.showPrice(sectionId));
        } catch (Exception e) {
            return Response.fail("展示报价失败");
        }
    }

    /**
     * 获取是否展示报价标识
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/isShowPrice", method = RequestMethod.GET)
    @ResponseBody
    public Response<Boolean> isShowPrice(@RequestParam("sectionId")String sectionId) {
        try {
            return Response.ok(beSectionParticipantService.isShowPrice(sectionId));
        } catch (Exception e) {
            return Response.fail("获取是否展示报价信息失败");
        }
    }

    /**
     * 获取中标候选单位列表
     * @param sectionId 标段Id
     * @return
     */
    @RequestMapping(value = "/getCandidateList", method = RequestMethod.GET)
    @ResponseBody
    public Response<List<BeSectionParticipant>> getCandidateList(@RequestParam("sectionId")String sectionId) {
        try {
            return Response.ok(beSectionParticipantService.getCandidateList(sectionId));
        } catch (Exception e){
            log.error(e.getMessage());
            return  Response.fail("获取中标候选单位列表失败");
        }
    }
}
